package org.openkanban.client.ui.presenter;

import org.openkanban.client.ui.footer.BoardContactTileGrid;
import org.openkanban.client.util.RootPanel;
import org.openkanban.client.util.StringUtils;

import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.tile.TileRecord;

/**
 * Callback that handles delete board performer request
 */
public class DeleteBoardPerformerCallback implements BooleanCallback {

    final Canvas source;

    public DeleteBoardPerformerCallback(Object source) {
	super();
	this.source = (Canvas) source;
    }

    public void execute(Boolean value) {
	if (value != null && value) {

	    Long currentBoardId = RootPanel.main.getBoard().getWhiteBoardView().getBoardDto().getBoardId();
	    BoardContactTileGrid bctg = (BoardContactTileGrid) source;
	    TileRecord tr = bctg.getSelectedRecord();
	    String userId = tr.getAttribute("userId");

	    if (StringUtils.isNumeric(userId) && currentBoardId != null && currentBoardId > 0) {
		PreparedRequest prepReq = new PreparedRequest("board", currentBoardId + "/contact/remove/" + userId);
		prepReq.sendAsyncRequest(new RemoveContactToBoardRequestCallback());
	    }
	}
    }
}